<template>
  <div>
    <div>
      <span>温馨提示：勾选功能之后将会显示在首页常用功能列表中，只对当前登录账号生效，快捷功能最多勾选10个;</span>
      <span>
        <el-button
          type="success"
          size="mini"
          @click="save"
        >保存</el-button>
      </span>
    </div>
    <el-tree
      :data="menuList"
      show-checkbox
      node-key="id"
      ref="tree"
      :props="defaultProps"
      :check-strictly="false"
    >
      <div slot-scope="{ node, data }">
        <span>{{ data.name }}</span>
      </div>
    </el-tree>

  </div>

</template>

<script>
import { oftenMenuSave, oftenMenuFindAll } from '@/api/after-sale/dashboard';

export default {
  data () {
    return {
      menuList: [],
      logicMenuList: [],
      noFatherId: [],
      defaultProps: {
        children: 'children',
        label: 'name',
      },
    }
  },
  methods: {
    //一键授权
    allAuthorization () {
      let c = []
      for (let item in this.menuList) {
        c.push(this.menuList[item].id)
      }
      this.$refs.tree.setCheckedKeys(c)
    },
    filtersFaterId (list) {
      list.map(item => {
        if (item.children && item.children.length > 0) {
          this.filtersFaterId(item.children)
        } else {
          this.noFatherId.push(item.id)
        }
      })
    },
    save () {
      let menuIds = this.$refs.tree.getCheckedKeys();
      this.filtersFaterId(this.menuList);
      let _menuIds = menuIds.filter(item => {
        if (this.noFatherId.includes(item)) {
          return item;
        }
      })
      if (_menuIds.length <= 10) {
        oftenMenuSave(_menuIds.join(',')).then(res => {
          let { success } = res;
          if (success) {
            this.$message.success('保存成功')
            this.$emit("close")
          }
          console.log('保存成功', res)
        })
      } else {
        this.$message.warning("最多勾选10个菜单")
      }

    },
    //过滤类型为按钮的菜单
    filter (menu = []) {
      let arry = [];
      menu.map(item => {
        if (item.children && item.children.length > 0) {
          item.children = this.filter(item.children);
        }
        if (item.type == 'MENU') {
          arry.push(item);
        }
      });
      return arry;
    }
  },
  async mounted () {
    let userInfo = JSON.parse(sessionStorage.getItem("login"))
    this.menuList = this.filter(userInfo.menus);
    let { data } = await oftenMenuFindAll();
    let menuids = data.map(item => { return item.id })
    this.$refs.tree.setCheckedKeys(menuids)
  },
}
</script>

<style lang="scss" scoped>
.el-table .delete-row {
  background: #f5f7fa;
  color: #ccc !important;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
.buttonStyle {
  text-align: center;
}
</style>
